import axios from 'axios'
import baseurl from '../http-config/config.js'
import VueCookies from 'vue-cookies'
import requestMethod from '../http-config/request-method.js'
import { Message } from 'element-ui'

const MuSicRequest = axios.create({
  baseURL: baseurl.wangyi,
  timeout: 1000 * 6,
  withCredentials: true,
})

MuSicRequest.interceptors.request.use(
  config => {
    // config.headers = {
    //   cookie: VueCookies.get('cookies') ? VueCookies.get('cookies').find((item) => item.indexOf('MUSIC_U') !== -1) : 'false',
    // }
    return config
  },
  error => {
    Message.error({
      message: '请求错误!!!',
      duration: 3000,
    })
    return Promise.reject(error)
  }
)

MuSicRequest.interceptors.response.use(
  // 对响应数据做点什么
  response => {
    // 这个是随便搞得 ，对于这个是要根据实际情况来定   实例一下
    switch (response.status) {
      case 200:
        if (response.data.code == 200) {
          response = response.data
        }
        // if (response.data.code == 403) {
        //   setTimeout(() => {
        //     window.location.href = '/login'
        //   }, 500)
        // }
        break
      case 503:
        alert('503 错误')
        break
    }
    return response
  },
  error => {
    if (error.message.includes('timeout')) {
      // 判断请求异常信息中是否含有超时timeout字符串
      Message.error({
        message: '请求超时！',
        duration: 3000,
      })
      return Promise.reject(error) // reject这个错误信息
    }
    Message.error({
      message: '响应错误!!!',
      duration: 3000,
    })
    return Promise.reject(error)
  }
)

const MuSicrequest = new requestMethod(MuSicRequest)
export default MuSicrequest
